//
//  CJShadowFilmTableViewController.m
//  KaiJing
//
//  Created by lanou on 16/1/15.
//  Copyright © 2016年 HuangZuCui. All rights reserved.
//

#import "CJShadowFilmTableViewController.h"
#import "PKRequestManager.h"
#import "CJShadowListTableViewCell.h"
#import <MJExtension.h>
#import "CJShadowMovieModel.h"
#import "CJPictorialCellWebViewViewController.h"
#import "HYMainMoviePlagerViewController.h"
#import "UMSocial.h"
#import <MJRefresh.h>
#import <MBProgressHUD.h>
#import "SZBaseNavController.h"
@interface CJShadowFilmTableViewController ()

@property (nonatomic, strong) CJShadowMovieModel *model;
@property (nonatomic, strong) NSMutableArray *sourceArray;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) MBProgressHUD *hud;
@end

@implementation CJShadowFilmTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
     self.navigationItem.title = @"影单详情";
    _sourceArray = [NSMutableArray array];
    self.model = [[CJShadowMovieModel alloc] init];
    _page = 1;
    [self requestData];
    [self hudMore];
    
    
    
    //刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
       
        _page = 1;
        [self requestData];
   
    }];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        _page += 1;
        [self requestData];
        
    }];
}


//菊花
-(void)hudMore
{
    self.hud = [[MBProgressHUD alloc] initWithView:self.view];
    _hud.frame = self.view.bounds;
    _hud.labelText = @"休息一下";
    _hud.minSize = CGSizeMake(100, 100);
    _hud.mode = MBProgressHUDModeIndeterminate;
    [self.view addSubview:_hud];
    [_hud show:YES];
}

- (void)requestData {
    
    NSString *urlStr = [NSString stringWithFormat:@"http://morguo.com/forum.php?mod=collection&action=view&ctid=%@&v=2&androidflag=1&appfrom=ios&iosversion=3.13&page=%ld",_ctid,_page];
    
    [PKRequestManager requestWithUrlString:urlStr parDic:nil method:GET finish:^(NSDictionary *dataDic) {
        
        [_hud hide:YES];
       
        if (_page == 1) {
            [_sourceArray removeAllObjects];
        }
        
        
        NSArray *array = dataDic[@"data"][@"list"];
        for (NSDictionary *dic in array) {
            CJShadowMovieListModel *model = [[CJShadowMovieListModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [_sourceArray addObject:model];
        }
        
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
        
        [self.tableView reloadData];
        
    } error:^(NSError *error) {
        
    }];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _sourceArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *idenfitier = @"CJShadowListTableViewCella";
    
    CJShadowListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:idenfitier];
    if (cell == nil) {
        cell = [[CJShadowListTableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:idenfitier];
    }
    
    CJShadowMovieListModel *model = _sourceArray[indexPath.row];
    cell.model = model;
    
    cell.imageButton.tag = 2400 + indexPath.row;
    [cell.imageButton addTarget:self action:@selector(clickOpenButton:) forControlEvents:(UIControlEventTouchUpInside)];

    
    //点赞
    cell.bottomView.praiseButton.tag = 11000 + indexPath.row;
    [cell.bottomView.praiseButton addTarget:self action:@selector(praiseButton:) forControlEvents:(UIControlEventTouchUpInside)];
    
    cell.bottomView.moreButton.tag = 12000 + indexPath.row;
    [cell.bottomView.moreButton addTarget:self action:@selector(moreButton:) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CJShadowMovieListModel *model = _sourceArray[indexPath.row];
    CGFloat height = [CJShadowListTableViewCell cellOfMessageLabelHeight:model];
    
    
    return (kScreenWidth - 15) / 2 * 3 / 2 + 70 + height;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CJShadowMovieListModel *model = _sourceArray[indexPath.row];
    
    CJPictorialCellWebViewViewController *webViewVC = [[CJPictorialCellWebViewViewController alloc] init];
    webViewVC.tid = model.itemdata.tid;
    
    [self.navigationController pushViewController:webViewVC animated:YES];
    
    
}



- (void)clickOpenButton:(UIButton *)button {
    
    
   
    CJShadowMovieListModel *model = _sourceArray[button.tag - 2400];
    
    
    HYMainMoviePlagerViewController *player = [[HYMainMoviePlagerViewController alloc] init];
    player.movie = model.itemdata.moviedata.trailerurl;
    
    [self.navigationController pushViewController:player animated:YES];
    
    
}


//点赞
- (void)praiseButton:(UIButton *)button {
    
    
    // 是否登录userName isEqualToString:@"YES"]
    if ([SZLoginTool shareLogin].isLogin) {
        
        button.selected = !button.selected;
        
        NSString *isLike = nil;
        if (button.selected) {
            isLike = @"like";
        } else {
            isLike = @"unlike";
        }
        
        
        CJShadowMovieListModel *model = _sourceArray[button.tag - 11000];
        
        NSString *str = [NSString stringWithFormat:@"http://morguo.com/forum.php?mod=threadlikecp&op=%@&tid=%@&formhash=%@&androidflag=1&appfrom=ios&iosversion=3.13",isLike,model.itemdata.tid, [SZLoginTool shareLogin].formhash];
        
        [PKRequestManager requestWithUrlString:str parDic:nil method:GET finish:^(NSDictionary *dataDic) {
            if (dataDic[@"data"][@"result"]) {
                SZLog(@"点赞成功");
                
                model.itemdata.isliked = button.selected;
            } else {
                SZLog(@"点赞失败");
                button.selected = !button.selected;
            }
            
            
        } error:^(NSError *error) {
            button.selected = !button.selected;
        }];
        
    } else {
        
        SZLoginViewController *loginVC = [[SZLoginViewController alloc] init];
        SZBaseNavController *loginNav = [[SZBaseNavController alloc] initWithRootViewController:loginVC];
        [self presentViewController:loginNav animated:YES completion:nil];
        
        
    }
}

//分享
- (void)moreButton:(UIButton *)button {
    
    CJShadowMovieListModel *model = _sourceArray[button.tag - 12000];

    NSString *url = [NSString stringWithFormat:@"http://morguo.com/forum.php?mod=viewthread&tid=%@&isfrommobile=1",model.itemdata.tid];
    
    UIWindow *window = [[UIApplication sharedApplication].delegate window];
    UIViewController *viewController = window.rootViewController;
    [UMSocialSnsService presentSnsIconSheetView:viewController
                                         appKey:@"569ee21fe0f55a76b10021e3"
                                      shareText:[NSString stringWithFormat:@"<<%@>>详情点击%@",model.itemdata.subject,url]
                                     shareImage:nil
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToEmail,UMShareToWechatFavorite,UMShareToWechatSession,UMShareToWechatTimeline,UMShareToQQ,UMShareToQzone,UMShareToTencent,nil]
                                       delegate:nil];
    
    
}









/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
